﻿using System;
using System.Linq;

namespace SalesSys.Comm.BLL
{
    /// <summary>
    /// 用户信息承载
    /// </summary>
    public class PrincipalInfo : IDisposable
    {
        public PrincipalInfo(Guid id, string name, Role[] roles, params RolePower[] powers)
        {
            Id = id;
            Name = name;
            Roles = roles;
            Powers = powers;

        }
        public Guid Id { get; private set; }
        /// <summary>
        /// 用户昵称
        /// </summary>
        public string Name { get; private set; }

      
        public RolePower[] Powers
        {
            get;
            private set;
        }
        public Role[] Roles
        {
            get;
            private set;
        }


        public void Dispose()
        {
            Powers = null;
            Roles = null;
        }

        /// <summary>
        /// 是否属于指定角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool IsInRole(string role)
        {
            return Roles == null ? false : Roles.Any(p => p.Name == role);
        }
        /// <summary>
        /// 查看是否有指定权限，暂不支持权限参数
        /// </summary>
        /// <param name="powerSign"></param>
        /// <returns></returns>
        public bool IsInPower(string powerSign)
        {
            return Powers == null ? false : Powers.Any(p => p.PowerName == powerSign);
        }
        public class Role
        {
            /// <summary>
            /// 角色标记名称
            /// </summary>
            public string Name { get; set; }
            //public RolePower[] Powers { get; set; }
        }
        public class RolePower
        {
            /// <summary>
            /// 权限标记名称
            /// </summary>
            public string PowerName { get; set; }
            public string TagParam { get; set; }
        }
    }
}
